package com.cai310.lottery.ticket.protocol.local.utils.jczq;

import java.util.HashMap;
import java.util.Map;

import com.cai310.lottery.entity.lottery.ticket.Ticket;
import com.cai310.lottery.support.jczq.ItemBF;
import com.cai310.lottery.support.jczq.ItemBQQ;
import com.cai310.lottery.support.jczq.ItemJQS;
import com.cai310.lottery.support.jczq.ItemSPF;
import com.cai310.lottery.support.jczq.JczqUtil;
import com.cai310.lottery.support.jczq.PlayType;
import com.cai310.lottery.ticket.protocol.local.disassemble.jczq.JczqPrintItemObj;
import com.cai310.lottery.ticket.protocol.response.dto.JcMatchOddsList;
import com.cai310.utils.JsonUtil;
import com.google.common.collect.Maps;

public  class JczqSpUtil{
	///{"awardList":[{"intTime":20111217,"lineId":20,"options":[{"award":2.8,"value":"0"}],"referenceValue":0},{"intTime":20111217,"lineId":21,"options":[{"award":1.4,"value":"3"},{"award":4.1,"value":"1"}],"referenceValue":0},{"intTime":20111217,"lineId":22,"options":[{"award":3.8,"value":"1"}],"referenceValue":0},{"intTime":20111217,"lineId":23,"options":[{"award":3.55,"value":"1"}],"referenceValue":0}],"index":0}
	public static JcMatchOddsList parseResponseSp(String responseMessage,JczqPrintItemObj jczqPrintItemObj,Ticket ticket){
		PlayType playType = PlayType.values()[jczqPrintItemObj.getPlayTypeOrdinal()];
		@SuppressWarnings("rawtypes")
		Map classMap = new HashMap();
		classMap.put("awardList", JczqSpItem.class);
		classMap.put("options", JczqItem.class);
		JczqSpItemObj jczqSpItemObj = JsonUtil.getObject4JsonString(responseMessage, JczqSpItemObj.class, classMap);
		Map<String, Double> awardMap;
		Map<String,Map<String, Double>> spMap = Maps.newHashMap();
		for(JczqSpItem jczqSpItem:jczqSpItemObj.getAwardList()){
			String matchKey = jczqSpItem.getIntTime()+"-"+JczqUtil.formatLineId(jczqSpItem.getLineId());
			awardMap = spMap.get(matchKey);
			for (JczqItem jczqItem : jczqSpItem.getOptions()) {
				if(null==awardMap||awardMap.isEmpty()){
					awardMap = Maps.newHashMap();
					awardMap.put(getAwardValueBySpText(jczqItem.getValue(), playType),jczqItem.getAward());
				}else{
					awardMap.put(getAwardValueBySpText(jczqItem.getValue(), playType),jczqItem.getAward());
				}
			}
			spMap.put(matchKey, awardMap);
		}
		JcMatchOddsList  jcMatchOddsList =  JczqUtil.bulidPrintAwardMap(spMap);
		return jcMatchOddsList;
	}
	public static String getAwardValueBySpText(String betValue,PlayType playType){
		String value = null;
				switch (playType) {
				case SPF:
					for (ItemSPF type : ItemSPF.values()) {
						if(betValue.equals(type.getValue())){
							value = type.getValue();
						}
					}
					break;
				case JQS:
					for (ItemJQS type : ItemJQS.values()) {
						if(betValue.equals(type.getValue())){
							value = type.getValue();
						}
					}
					break;
				case BF:
					for (ItemBF type : ItemBF.values()) {
						if(type.getValue().equals(betValue)){
							value = type.getValue();
						}
					}
					break;
				case BQQ:
					for (ItemBQQ type : ItemBQQ.values()) {
						if(type.getValue().equals(betValue)){
							value = type.getValue();
						}
					}
					break;
				default:
					throw new RuntimeException("玩法不正确.");
				}
		return value;
	}
}
